package com.project.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.project.domain.entity.BizProjectBudgetAlteration;
import com.project.domain.vo.BudgetAlterationHistoryVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface AlterationBudgetMapper extends BaseMapper<BizProjectBudgetAlteration> {

    /**
     * 根据项目号获取预算变更记录
     * @param proId
     * @return
     */
    @Select({
            "<script>",
            "SELECT  ",
            "    bpa.SUBJECT_CODE as id,   ",
            "    FirstAlteration.BEFORE_ALTER_BUDGET_FEE / 10000 AS initVal,   ",
            "    (SELECT GROUP_CONCAT(bpa2.BUDGET_FEE_CHANGE / 10000 SEPARATOR '; ')   ",
            "    FROM biz_project_budget_alteration bpa2  ",
            "    JOIN BIZ_PROJECT_ALTERATION bpalt2 ON bpa2.PROJECT_ALTERATION_ID = bpalt2.ID  ",
            "    WHERE bpa2.SUBJECT_CODE = bpa.SUBJECT_CODE AND bpalt2.PROJECT_ID = bpalt.PROJECT_ID   ",
            "    AND bpalt2.CHECKSTATUS = 2 ",
            "    ORDER BY bpalt2.ALTER_DATE) AS changeMoneyHis,   ",
            "    (SELECT GROUP_CONCAT(DATE_FORMAT(bpalt2.ALTER_DATE, '%Y-%m-%d %H:%i:%s') SEPARATOR '; ')   ",
            "    FROM biz_project_budget_alteration bpa2   ",
            "    JOIN BIZ_PROJECT_ALTERATION bpalt2 ON bpa2.PROJECT_ALTERATION_ID = bpalt2.ID  ",
            "    WHERE bpa2.SUBJECT_CODE = bpa.SUBJECT_CODE AND bpalt2.PROJECT_ID = bpalt.PROJECT_ID   ",
            "    AND bpalt2.CHECKSTATUS = 2 ",
            "    ORDER BY bpalt2.ALTER_DATE) AS changeTimeHis   ",
            "FROM  ",
            "    biz_project_budget_alteration bpa   ",
            "JOIN  ",
            "    BIZ_PROJECT_ALTERATION bpalt ON bpa.PROJECT_ALTERATION_ID = bpalt.ID   ",
            "LEFT JOIN (  ",
            "    SELECT   ",
            "        bpa_inner.SUBJECT_CODE,bpalt_inner.PROJECT_ID,bpa_inner.BEFORE_ALTER_BUDGET_FEE,  ",
            "        ROW_NUMBER() OVER (PARTITION BY bpa_inner.SUBJECT_CODE, bpalt_inner.PROJECT_ID ORDER BY bpalt_inner.ALTER_DATE) AS rn  ",
            "    FROM  ",
            "        biz_project_budget_alteration bpa_inner   ",
            "    JOIN   ",
            "        BIZ_PROJECT_ALTERATION bpalt_inner ON bpa_inner.PROJECT_ALTERATION_ID = bpalt_inner.ID   ",
            "    WHERE bpalt_inner.CHECKSTATUS = 2 ",
            ") AS FirstAlteration ON bpa.SUBJECT_CODE = FirstAlteration.SUBJECT_CODE   ",
            "                     AND bpalt.PROJECT_ID = FirstAlteration.PROJECT_ID   ",
            "                     AND FirstAlteration.rn = 1   ",
            "WHERE   ",
            "    bpalt.PROJECT_ID = '${proId}'   ",
            "GROUP BY   ",
            "    bpa.SUBJECT_CODE, FirstAlteration.BEFORE_ALTER_BUDGET_FEE, bpalt.PROJECT_ID",
            "</script>"
    })
    List<BudgetAlterationHistoryVo> getBudgetChangeListByProId(@Param("proId") String proId);
}
